package cc.rengu.igas.bomp.biz.service.impl;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.facade.BompQrCodeManageService;
import cc.rengu.igas.bomp.facade.request.MchntQrCodeApplyRequest;
import cc.rengu.igas.bomp.facade.request.MchntQrCodeBatchApplyRequest;
import cc.rengu.igas.bomp.facade.request.MchntQrCodeManageRequest;
import cc.rengu.igas.bomp.facade.request.MchntQrCodeQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntQrCodeApplyResponse;
import cc.rengu.igas.bomp.facade.response.MchntQrCodeBatchApplyResponse;
import cc.rengu.igas.bomp.facade.response.MchntQrCodeManageResponse;
import cc.rengu.igas.bomp.facade.response.MchntQrCodeQueryResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class BompQrCodeManageServiceImpl implements BompQrCodeManageService {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<MchntQrCodeApplyResponse> mchntQrCodeApply(MchntQrCodeApplyRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntQrCodeApplyResponse> result = new Result<>();
        MchntQrCodeApplyResponse response = new MchntQrCodeApplyResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MchntQrCodeApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MchntQrCodeApplyResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("生成商户收款聚合码处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<MchntQrCodeBatchApplyResponse> mchntQrCodeBatchApply(MchntQrCodeBatchApplyRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntQrCodeBatchApplyResponse> result = new Result<>();
        MchntQrCodeBatchApplyResponse response = new MchntQrCodeBatchApplyResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MchntQrCodeBatchApplyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MchntQrCodeBatchApplyResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("批量生成商户收款聚合码处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<MchntQrCodeQueryResponse> mchntQrCodeQuery(MchntQrCodeQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntQrCodeQueryResponse> result = new Result<>();
        MchntQrCodeQueryResponse response = new MchntQrCodeQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MchntQrCodeQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MchntQrCodeQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户收款聚合码查询处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<MchntQrCodeManageResponse> mchntQrCodeManage(MchntQrCodeManageRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntQrCodeManageResponse> result = new Result<>();
        MchntQrCodeManageResponse response = new MchntQrCodeManageResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MchntQrCodeManageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MchntQrCodeManageResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户收款聚合码管理处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }
}
